<template>
	<view id="add">
		<view class="list">
			<view><text>诊疗时间</text><text @click="$refs.date.show()">{{time}}</text></view>
			<view 
				v-for="(item,index) in ZlObj"
				:key='index'
			>
				<text>{{item.name}}</text>
				<view>
					<text>{{item.cont}}</text>
					<text class="del" @click="del(index)">删除</text>
				</view>
			</view>
		</view>
		
		<view class="addBtn submit" @click="modelPage = true">添加诊疗信息</view>
		
		
		<textarea v-model="intro" placeholder="请输入描述内容" />
		
		
		<!-- <view class="msg">
			相关参数相关参数相关参数相关参数相关参数相关参数相关参数相关参数相关参数相关参数相关参数相关参数相关参数
		</view> -->
		
		<view class="uploadImg">
			<text>图片上传</text>
			<view>
				<image v-for="(item,index) in imgs" :key='index' :src="item" @longpress='delimg(index)'></image>
				<image @click="chooseImg" :src="vuex_httpImageUrl && vuex_httpImageUrl+'static/common/添加照片.png'" mode="widthFix"></image>
			</view>
		</view>
		
		<view class="submit" @click="C_submit">确定并提交</view>
		
		<view class="model" v-if="modelPage" @click="modelPage = false">
			<div class="cont" @click.stop>
				<input type="text" v-model="modelForm.name" placeholder="请输入名称"/>
				<input type="text" v-model="modelForm.cont" placeholder="请输入内容"/>
				<view class="submit" @click="modelSubmit">确定</view>
			</div>
		</view>
		
		 <w-picker
		        :visible.sync="visible"
		        mode="date" 
		        :current="true"
		        fields="day"
		        @confirm="onConfirm($event,'date')"
		        @cancel="visible = false"
		        :disabled-after="false"
		        ref="date" 
		    ></w-picker>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				visible:false,
				
				statusIndex:null,
				
				// 诊疗数据
				ZlObj:[],
				
				modelPage:false,
				modelForm:{name:'',cont:''},
				
				time:'请选择诊疗时间',
				intro:'',
				imgs:[]
			};
		},
		onLoad(o) {
			let {name:title,index} = JSON.parse(o.obj)
			this.statusIndex = index
			uni.setNavigationBarTitle({title})
		},
		methods:{
			delimg(index){
				uni.showModal({
					title:'提示',
					content:'确定删除该图片',
					success:res=>{
						if(res.confirm){
							this.imgs.splice(index,1)
						}
					}
				})
			},
			chooseImg(){
				uni.chooseImage({
					count:9,
					success:res=>{
						this.imgs = res.tempFilePaths
					}
				})
			},
			
			C_submit(){
				if(this.time == '请选择诊疗时间'){this.$u.toast('请选择诊疗时间');return};
				uni.showLoading()
				let imgs = []
				if(this.imgs.length){
					this.imgs.forEach((item,index) =>{
						uni.uploadFile({
							url:this.$u.http.config.baseUrl+'/upload/up',
							filePath:item,
							name:'file',
							success:res=>{
								imgs.push(JSON.parse(res.data).data.all_image)
								if(this.imgs.length == imgs.length){
									uni.hideLoading()
									this.$u.post('/history/add_history',{
										status:this.statusIndex,
										images:imgs.toString(),
										treattime:this.time,
										intro:this.intro,
										json:JSON.stringify(this.ZlObj)
									}).then(res=>{
										console.log(res);
										if(res.code == 0){
											this.$u.toast(res.msg)
											setTimeout(_=>{
												uni.navigateBack()
											},700)
										}
									})
								}
							},
						})
					})
				}else{
					uni.hideLoading()
					this.$u.post('/history/add_history',{
						status:this.statusIndex,
						images:'',
						treattime:this.time,
						intro:this.intro,
						json:JSON.stringify(this.ZlObj)
					}).then(res=>{
						console.log(res);
						if(res.code == 0){
							this.$u.toast(res.msg)
							setTimeout(_=>{
								uni.navigateBack()
							},700)
						}
					})
				}
			},
			onConfirm(val){
				this.time = val.value
			},
			modelSubmit(){
				let { name , cont } = this.modelForm;
				if(!name){this.$u.toast('请输入名称');return}
				if(!cont){this.$u.toast('请输入内容');return}
				
				let modelFrom = JSON.parse(JSON.stringify(this.modelForm))
				
				this.ZlObj.unshift(modelFrom)
				this.modelPage = false;
				this.modelForm.name = this.modelForm.cont = ''
			},
			del(index){
				uni.showModal({
					title:'提示',
					content:'确定要删除吗?',
					success:res=>{
						!res.confirm || this.ZlObj.splice(index,1)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	#add{
		margin-top: 20rpx;
		overflow: hidden;
		textarea{
			background-color: #FFFFFF;
			width: 95%;
			padding: 20rpx;
			box-sizing: border-box;
			margin: auto;
			margin-bottom: 20rpx;
		}
		.list{
			padding: 0 40rpx;
			border-radius: 20rpx;
			width: 95%;
			margin: auto;
			background-color: #FFFFFF;
			margin-bottom: 20rpx;
			>view{
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 100rpx;
				border-bottom: 1px solid #F1F1F1;
				.del{
					color: red;
					margin-left: 10px;
				}
			}
			.msg{
				margin-top: 30rpx;
				padding: 40rpx;
				height: auto;
				border-radius: 20rpx;
				background-color: #F8F8F8;
				border: 1px solid #EAEAEA;
			}
		}
		.uploadImg{
			background-color: #FFFFFF;
			width: 95%;
			padding: 20rpx;
			box-sizing: border-box;
			margin: auto;
			margin-bottom: 20rpx;
			>text{
				display: block;
				margin-bottom: 20rpx;
			}
			image{
				width: 160rpx;
				height: 160rpx;
			}
		}
		.addBtn.submit{
			display: inline-block;
			width: auto;
			border-radius: 10rpx;
			margin: 0;
			margin-left:20rpx ;
			margin-bottom:20rpx ;
		}
		.submit{
			width: 80%;
			margin: 50rpx auto;
			text-align: center;
			padding: 20rpx;
			border-radius: 50rpx;
		}
		.model{
			position: fixed;
			top: 0;left: 0;right: 0;bottom: 0;
			background-color: rgba(0,0,0,.5);
			display: flex;
			align-items: center;
			justify-content: center;
			z-index: 10;
			.cont{
				background-color: #FFFFFF;
				border-radius: 10rpx;
				padding: 20rpx;
				width: 80%;
				input{
					border: 1px solid rgba(0,0,0,.2);
					height: 100rpx;
					margin-bottom: 20rpx;
					padding-left: 50rpx;
					border-radius: 20rpx;
				}
				.submit{
					width: 100%;
					margin: 10px auto 0;
				}
			}
		}
	}
</style>
